package com.greenant.store.utils;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SettingUtils {
    private static final String SP_SETTINGS = "settings";
    private static final String KEY_DOWNLOAD_ONLY_WIFI = "DOWNLOAD_ONLY_WIFI";
    private static final String KEY_NO_PICTURE_MODE = "NO_PICTURE_MODE";
    private static final String KEY_AUTO_UPDATE_ONLY_WIFI = "AUTO_UPDATE_ONLY_WIFI";
    private static final String KEY_AUTO_DELETE_APK = "AUTO_DELETE_APK";
    private static final String KEY_NOTIFY_SOFT_UPDATE = "NOTIFY_SOFT_UPDATE";
    private static final String KEY_NOTIFY_GAME_APP = "NOTIFY_GAME_APP";
    private static final String KEY_LAST_UPDATE_TIME = "LAST_UPDATE_TIME";
    private static final String KEY_LAST_APP_VERSION = "LAST_APP_VERSION";
    private static final String KEY_NEED_SHOW_STARTUPPAGE_NEXT_START = "NEED_SHOW_STARTUPPAGE_NEXT_START";
    private static final String KEY_SKIP_UPDATE_APP = "SKIP_UPDATE_APP";
    private static final String KEY_LAST_NOTIFY_UPDATE_DATE = "LAST_NOTIFY_UPDATE_DATE";
    private static final String KEY_NEED_SHOW_NET_NOTICE = "NEED_SHOW_NET_NOTICE";
    
    /**
     * 仅在WIFI下下载
     * @param context
     * @param downloadOnlyWifi
     */
    public static void setAllowNotification(Context context, boolean downloadOnlyWifi) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_DOWNLOAD_ONLY_WIFI, downloadOnlyWifi);
        editor.commit();
    }
    
    /**
     * 仅在WIFI下下载
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isAllowNotification(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_DOWNLOAD_ONLY_WIFI)) {
            setAllowNotification(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_DOWNLOAD_ONLY_WIFI, defValue);
    }
    
    /**
     * 不下载图片应用截图
     * @param context
     * @param noPictureMode
     */
    public static void setAllowNotificationSound(Context context, boolean noPictureMode) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_NO_PICTURE_MODE, noPictureMode);
        editor.commit();
    }
    
    /**
     * 不下载图片应用截图
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isAllowNotificationSound(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_NO_PICTURE_MODE)) {
            setAllowNotificationSound(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_NO_PICTURE_MODE, defValue);
    }
    
    /**
     * 仅在WIFI下手机空闲时自动下载更新包
     * @param context
     * @param autoUpdateOnlyWifi
     */
    public static void setAutoUpdateOnlyWifi(Context context, boolean autoUpdateOnlyWifi) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_AUTO_UPDATE_ONLY_WIFI, autoUpdateOnlyWifi);
        editor.commit();
    }
    
    /**
     * 仅在WIFI下手机空闲时自动下载更新包
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isAutoUpdateOnlyWifi(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_AUTO_UPDATE_ONLY_WIFI)) {
            setAutoUpdateOnlyWifi(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_AUTO_UPDATE_ONLY_WIFI, defValue);
    }
    
    /**
     * 安装后删除安装包
     * @param context
     * @param autoDeleteApk
     */
    public static void setAutoDeleteApk(Context context, boolean autoDeleteApk) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_AUTO_DELETE_APK, autoDeleteApk);
        editor.commit();
    }
    
    /**
     * 安装后删除安装包
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isAutoDeleteApk(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_AUTO_DELETE_APK)) {
            setAutoDeleteApk(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_AUTO_DELETE_APK, defValue);
    }
    
    /**
     * 软件更新提示
     * @param context
     * @param notifySoftUpdate
     */
    public static void setNotifySoftUpdate(Context context, boolean notifySoftUpdate) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_NOTIFY_SOFT_UPDATE, notifySoftUpdate);
        editor.commit();
    }
    
    /**
     * 软件更新提示
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isNotifySoftUpdate(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_NOTIFY_SOFT_UPDATE)) {
            setNotifySoftUpdate(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_NOTIFY_SOFT_UPDATE, defValue);
    }
    
    /**
     * 在通知栏提示优质游戏和应用
     * @param context
     * @param notifyGameApp
     */
    public static void setNotifyGameApp(Context context, boolean notifyGameApp) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_NOTIFY_GAME_APP, notifyGameApp);
        editor.commit();
    }
    
    /**
     * 在通知栏提示优质游戏和应用
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isNotifyGameApp(Context context, boolean defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if (!sp.contains(KEY_NOTIFY_GAME_APP))
        {
            setNotifyGameApp(context, defValue);
            return defValue;
        }
        boolean flag = sp.getBoolean(KEY_NOTIFY_GAME_APP, defValue);
        return flag;
    }
    
   
    /**
     * 最后一次更新时间
     * @param context
     * @param lastUpdateTime
     */
    public static void setLastUpdateTime(Context context, long lastUpdateTime) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putLong(KEY_LAST_UPDATE_TIME, lastUpdateTime);
        editor.commit();
    }
    
    /**
     * 最后一次更新时间
     * @param context
     * @param defValue
     * @return
     */
    public static long getLastUpdateTime(Context context, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_LAST_UPDATE_TIME)) {
            setLastUpdateTime(context, defValue);
            return defValue;
        }
        return sp.getLong(KEY_LAST_UPDATE_TIME, defValue);
    }
    
    /**
     * 最后一次软件版本
     * @param context
     * @param lastAppVersion
     */
    public static void setLastAppVersion(Context context, String lastAppVersion) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(KEY_LAST_APP_VERSION, lastAppVersion);
        editor.commit();
    }
    
    /**
     * 最后一次软件版本
     * @param context
     * @param defValue
     * @return
     */
    public static String getLastAppVersion(Context context, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_LAST_APP_VERSION)) {
            setLastAppVersion(context, defValue);
            return defValue;
        }
        return sp.getString(KEY_LAST_APP_VERSION, defValue);
    }
    
    /**
     * 需要展示启动页
     * @param context
     * @param notifyGameApp
     */
    public static void setNeedShowStartupPage(Context context, boolean needShowStartupPage) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_NEED_SHOW_STARTUPPAGE_NEXT_START, needShowStartupPage);
        editor.commit();
    }
    
    /**
     * 需要展示启动页
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isNeedShowStartupPage(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_NEED_SHOW_STARTUPPAGE_NEXT_START)) {
            setNotifyGameApp(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_NEED_SHOW_STARTUPPAGE_NEXT_START, defValue);
    }
    
    /**
     * 添加忽略更新
     * @param context
     * @param notifyGameApp
     */
    public static int addSkipUpdateApp(Context context, String packageName) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        Set<String> set = getSkipedApps(context, new HashSet<String>());
        set.add(packageName);
        editor.putStringSet(KEY_SKIP_UPDATE_APP, set);
        editor.commit();
        return set.size();
    }
    
    /**
     * 删除忽略更新
     * @param context
     * @param notifyGameApp
     */
    public static int removeSkipUpdateApp(Context context, String packageName) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        Set<String> set = getSkipedApps(context, new HashSet<String>());
        set.remove(packageName);
        editor.putStringSet(KEY_SKIP_UPDATE_APP, set);
        editor.commit();
        return set.size();
    }
    
    /**
     * 需要展示启动页
     * @param context
     * @param defValue
     * @return
     */
    public static Set<String> getSkipedApps(Context context, Set<String> defValues) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_SKIP_UPDATE_APP)) {
            Editor editor = sp.edit();
            editor.putStringSet(KEY_SKIP_UPDATE_APP, defValues);
            return defValues;
        }
        return sp.getStringSet(KEY_SKIP_UPDATE_APP, defValues);
    }
    
    /**
     * 最后通知更新时间
     * @param context
     * @param lastAppVersion
     */
    public static void setLastNotifyUpdateDate(Context context, String lastNotifyUpdateDate) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(KEY_LAST_NOTIFY_UPDATE_DATE, lastNotifyUpdateDate);
        editor.commit();
    }
    
    /**
     * 最后通知更新时间
     * @param context
     * @param defValue
     * @return
     */
    public static String getLastNotifyUpdateDate(Context context, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_LAST_NOTIFY_UPDATE_DATE)) {
            setLastAppVersion(context, defValue);
            return defValue;
        }
        return sp.getString(KEY_LAST_NOTIFY_UPDATE_DATE, defValue);
    }
    
    /**
     * 需要展示网络提示
     * @param context
     * @param notifyGameApp
     */
    public static void setNeedShowNetNotice(Context context, boolean needShowNetNotice) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(KEY_NEED_SHOW_NET_NOTICE, needShowNetNotice);
        editor.commit();
    }
    
    /**
     * 需要展示网络提示
     * @param context
     * @param defValue
     * @return
     */
    public static boolean isNeedShowNetNotice(Context context, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SP_SETTINGS, Context.MODE_PRIVATE);
        if(!sp.contains(KEY_NEED_SHOW_NET_NOTICE)) {
        	setNeedShowNetNotice(context, defValue);
            return defValue;
        }
        return sp.getBoolean(KEY_NEED_SHOW_NET_NOTICE, defValue);
    }
}
